package com.eascs.ticket.ticket.date;

import java.util.Date;

import com.eascs.ticket.configuration.entity.TicketStatusConstant;
import com.eascs.ticket.configuration.entity.TicketTypeConstant;

public class TicketDueDateHelper {
	public static Date buildTicketDueDate(TicketDueDate ticketDueDate) {

		// String status = ticketDueDate.getStatus();
		String ticketTypeCode = ticketDueDate.getTicketTypeCode();
		String ticketStatusType = ticketDueDate.getTicketStatusType();

		Date returnDate = null;
		if (TicketStatusConstant.TICKET_STATUS_TYPE_CLOSE.equals(ticketStatusType)) {
			returnDate = new Date(0);
		} else if (TicketTypeConstant.TICKET_TYPE_CODE_LEAVE.equals(ticketTypeCode)) {
			returnDate = ticketDueDate.getStartTime();
		} else if (TicketTypeConstant.TICKET_TYPE_CODE_OVERTIME.equals(ticketTypeCode)) {
			returnDate = ticketDueDate.getEndTime();
		} else if (TicketTypeConstant.TICKET_TYPE_CODE_TASK.equals(ticketTypeCode) || TicketTypeConstant.TICKET_TYPE_CODE_JOBLOG.equals(ticketTypeCode) || TicketTypeConstant.TICKET_TYPE_CODE_WORKLOG.equals(ticketTypeCode) || TicketTypeConstant.TICKET_TYPE_CODE_REPORTLOG.equals(ticketTypeCode)) {
			returnDate = ticketDueDate.getImplementationReviewTime();
			if (TicketStatusConstant.TICKET_STATUS_TYPE_QA.equals(ticketStatusType) && null != ticketDueDate.getQaValidatedTime()) {
				returnDate = ticketDueDate.getQaValidatedTime();
			}
		} else if (TicketTypeConstant.TICKET_TYPE_CODE_WORKLOG.equals(ticketTypeCode)) {
			returnDate = ticketDueDate.getImplementationReviewTime();
		} else if (TicketTypeConstant.TICKET_TYPE_CODE_SUB_TASK.equals(ticketTypeCode)) {
			returnDate = ticketDueDate.getImplementationReviewTime();
		} else if (TicketTypeConstant.TICKET_TYPE_CODE_VERSION.equals(ticketTypeCode)) {
			returnDate = ticketDueDate.getPublishProdTime();
		} else if (TicketTypeConstant.TICKET_TYPE_CODE_BUG.equals(ticketTypeCode)) {
			if (TicketStatusConstant.TICKET_STATUS_TYPE_PM.equals(ticketStatusType) || TicketStatusConstant.TICKET_STATUS_TYPE_DEV.equals(ticketStatusType)) {
				returnDate = ticketDueDate.getImplementationReviewTime();
			}
		} else if (TicketStatusConstant.TICKET_STATUS_TYPE_PM.equals(ticketStatusType)) {
			returnDate = ticketDueDate.getRequirementReviewedTime();
		} else if (TicketStatusConstant.TICKET_STATUS_TYPE_DEV.equals(ticketStatusType)) {
			returnDate = ticketDueDate.getImplementationReviewTime();
			if (null == returnDate) {
				returnDate = ticketDueDate.getImplementationDoneTime();
			}
		}

		if (null == returnDate) {
			returnDate = ticketDueDate.getVersionPublishProdTime();
		}
		if (null == returnDate) {
			returnDate = ticketDueDate.getQaValidatedTime();
		}
		if (null == returnDate) {
			returnDate = ticketDueDate.getImplementationReviewTime();
		}
		if (null == returnDate) {
			returnDate = ticketDueDate.getImplementationDoneTime();
		}
		if (null == returnDate) {
			returnDate = ticketDueDate.getDueDate();
		}

		return returnDate;
	}
}
